package ru.ifmo.cppsc.workers;


import ru.ifmo.cppsc.grammar.Symbol;
import ru.ifmo.cppsc.grammar.Grammar;
import ru.ifmo.cppsc.grammar.Production;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Arrays;

/**
 * Author: Olga Komaleva
 * Date: Jun 20, 2008
 */
public class FileWorker {
    public static void saveGrammar(String fileName, Grammar grammar) {
        BufferedWriter f = null;

        try {
            f = new BufferedWriter(new FileWriter(fileName));
            String termStr = "";
            for (Symbol term: grammar.getTerminals()) {
                termStr += term.getName() + " ";
            }
            f.write(termStr + "\n");

            String nontermStr = "";
            for (Symbol nonterm: grammar.getNonTerminals()) {
                nontermStr += nonterm.getName() + " ";
            }
            f.write(nontermStr + "\n");

            ArrayList<String> prodsStr = new ArrayList<String>();

            for (Symbol nonterm: grammar.getNonTerminals()) {
                String prodStr = "";
                for (Production p: grammar.getProductions()) {
                    if (p.getLeft() == nonterm) {
                        if (prodStr.equals("")) {
                            prodStr += nonterm.getName() + " ";
                        } else {
                            prodStr += "| ";
                        }
                        for (int j = 0; j < p.getRight().length; j++) {
                            prodStr += p.getRight()[j].getName() + " ";
                        }
                    }
                }
                prodsStr.add(prodStr);
            }

            for (String s : prodsStr) {
                f.write(s + "\n");
            }

        } catch (Exception e) {
            try {
                e.printStackTrace();
                f.close();
            } catch (Exception ee) {
            }
        } finally {
            try {
                f.close();
            } catch (Exception ee) {
            }
        }
    }

    public static ArrayList<String> read(String str) {

        String[] ss = str.split( "[ ]");
        ArrayList<String> list = new ArrayList<String>();
        list.addAll(Arrays.asList(ss));
        return list;
        
        /*return new ArrayList ();
        ArrayList<String> symbolNames = new ArrayList<String>();
        int i = 0;
        char ch;
        String s = "";

        while (i < str.length()) {
            ch = str.charAt(i);
            if (ch == ' ') {
                symbolNames.add(s);
                s = "";
            } else {
                s += ch;
            }
            i++;
        }
        if (s.length() > 0) {
            symbolNames.add(s);
        }

        return symbolNames;*/
    }

    public static void saveTable(String fileName, String[][] table) {
        BufferedWriter f = null;

        try {
            f = new BufferedWriter(new FileWriter(fileName));
            for (int i = 0; i < table.length; i++) {
                String s = "";
                for (int j = 0; j < table.length; j++) {
                    s += table[i][j] + " ";
                }
                s += "\n";
                f.write(s);
            }
        } catch (Exception e) {
            try {
                e.printStackTrace();
                f.close();
            } catch (Exception ee) {
            }
        } finally {
            try {
                f.close();
            } catch (Exception ee) {
            }
        }
    }

    public static String[][] readTable(String fileName) {
        BufferedReader f = null;
        ArrayList<String[]> table = new ArrayList<String[]>();

        try {
            f = new BufferedReader(new FileReader(fileName));
            String s;
            while ((s = f.readLine()) != null) {
               ArrayList<String> a = FileWorker.read(s);
               table.add(a.toArray(new String[a.size()]));
            }
        } catch (Exception e) {
            try {
                e.printStackTrace();
                f.close();
            } catch (Exception ee) {
            }
        } finally {
            try {
                f.close();
            } catch (Exception ee) {
            }
        }

        return table.toArray(new String[table.size()][table.size()]);
    }
}
